Imports CapaDeDatos
Imports System.Web.UI.WebControls

Public Class nMenu
    Private menu As CapaDeDatos.dMenu = New CapaDeDatos.dMenu
    Private Login As CapaDeNegocios.nLogin = New CapaDeNegocios.nLogin
    Public Function ObtenerMenuGrupo(ByVal mnuPrincipal As Menu, ByVal Usuario As String) As Menu

        Dim dts As DataSet
        dts = menu.Buscar(Login.GrupoUsuario(Usuario).Tables(0).Rows(0)("IdTipoUsuario").ToString())

        For Each drMenuItem As Data.DataRow In dts.Tables(0).Rows
            If drMenuItem("MenuId").Equals(drMenuItem("PadreId")) Then
                Dim mnuMenuItem As New MenuItem
                mnuMenuItem.Value = drMenuItem("MenuId").ToString
                mnuMenuItem.Text = drMenuItem("descripcion").ToString
                'mnuMenuItem.ImageUrl = drMenuItem("Icono").ToString
                mnuMenuItem.NavigateUrl = drMenuItem("Url").ToString
                mnuPrincipal.Items.Add(mnuMenuItem)
                AddMenuItem(mnuMenuItem, dts.Tables(0))
            End If
        Next
        Return mnuPrincipal
    End Function


    Public Function ObtenerMenu(ByVal mnuPrincipal As Menu) As Menu
        '        Dim mnuPrincipal As Menu = New Menu()
        Dim dts As DataSet
        dts = menu.ObtieneMenu()
        For Each drMenuItem As Data.DataRow In dts.Tables(0).Rows
            If drMenuItem("MenuId").Equals(drMenuItem("PadreId")) Then
                Dim mnuMenuItem As New MenuItem
                mnuMenuItem.Value = drMenuItem("MenuId").ToString
                mnuMenuItem.Text = drMenuItem("descripcion").ToString
                ' mnuMenuItem.ImageUrl = drMenuItem("Icono").ToString
                mnuMenuItem.NavigateUrl = drMenuItem("Url").ToString
                mnuPrincipal.Items.Add(mnuMenuItem)
                AddMenuItem(mnuMenuItem, dts.Tables(0))
            End If
        Next
        Return mnuPrincipal
    End Function

    Private Sub AddMenuItem(ByRef mnuMenuItem As MenuItem, ByVal dtMenuItems As Data.DataTable)
        For Each drMenuItem As Data.DataRow In dtMenuItems.Rows
            If drMenuItem("PadreId").ToString.Equals(mnuMenuItem.Value) AndAlso _
            Not drMenuItem("MenuId").Equals(drMenuItem("PadreId")) Then
                Dim mnuNewMenuItem As New MenuItem
                mnuNewMenuItem.Value = drMenuItem("MenuId").ToString
                mnuNewMenuItem.Text = drMenuItem("descripcion").ToString
                ' mnuNewMenuItem.ImageUrl = drMenuItem("Icono").ToString
                mnuNewMenuItem.NavigateUrl = drMenuItem("Url").ToString
                mnuMenuItem.ChildItems.Add(mnuNewMenuItem)
                AddMenuItem(mnuNewMenuItem, dtMenuItems)
            End If
        Next
    End Sub
End Class
